#!/bin/bash

echo "Testing EbbFlow WaterBear Component Only"
echo "========================================"
echo "Test Configuration:"
echo "  - Nodes: 4"
echo "  - Duration: 30 seconds"
echo "  - Component: WaterBear only (no ADKR, no Epoch management)"
echo ""

# Clean up any existing processes
pkill -f ebbflow 2>/dev/null || true
pkill -f waterbear 2>/dev/null || true
sleep 2

# Create logs directory
mkdir -p logs

echo "=== Testing EbbFlow WaterBear Only ==="

# Start WaterBear-only servers
echo "Starting WaterBear-only servers..."
for i in {0..3}; do
    echo "Started WaterBear-only server $i"
    ./bin/waterbear_only_server -id $i > logs/waterbear_server_${i}.log 2>&1 &
    sleep 1
done

# Wait for servers to start
sleep 3

# Start client
echo "Starting EbbFlow client..."
./bin/ebbflow_client -config etc/conf.json -duration 30 -batchsize 1000 -txsize 250 > logs/waterbear_client.log 2>&1 &

# Wait for test to complete
echo "Running WaterBear test for 30 seconds..."
sleep 35

# Stop processes
echo "Stopping WaterBear processes..."
pkill -f waterbear_only_server 2>/dev/null || true
pkill -f ebbflow 2>/dev/null || true
sleep 2

echo ""
echo "=== WaterBear Only Performance Analysis ==="

# Analyze server logs
echo "Server Logs Analysis:"
echo "--------------------"
for i in {0..3}; do
    if [ -f "logs/waterbear_server_${i}.log" ]; then
        echo "Server $i:"
        
        # Count WaterBear activities
        waterbear_count=$(grep -c "WaterBear" logs/waterbear_server_${i}.log 2>/dev/null || echo "0")
        consensus_count=$(grep -c "consensus" logs/waterbear_server_${i}.log 2>/dev/null || echo "0")
        crash_count=$(grep -c "panic" logs/waterbear_server_${i}.log 2>/dev/null || echo "0")
        
        echo "  - WaterBear Activities: $waterbear_count"
        echo "  - Consensus Activities: $consensus_count"
        echo "  - Crashes: $crash_count"
        
        # Check for specific WaterBear messages
        if grep -q "Starting WaterBear" logs/waterbear_server_${i}.log 2>/dev/null; then
            echo "  - WaterBear Started: Yes"
        else
            echo "  - WaterBear Started: No"
        fi
        
        if grep -q "WaterBear consensus" logs/waterbear_server_${i}.log 2>/dev/null; then
            echo "  - WaterBear Consensus: Yes"
        else
            echo "  - WaterBear Consensus: No"
        fi
        
        echo ""
    fi
done

# Analyze client logs
if [ -f "logs/waterbear_client.log" ]; then
    echo "Client Logs Analysis:"
    echo "-------------------"
    
    # Extract transaction info
    tx_submitted=$(grep "Submitted:" logs/waterbear_client.log | tail -1 | grep -o '[0-9]*' | head -1 || echo "0")
    tx_time=$(grep "Time:" logs/waterbear_client.log | tail -1 | grep -o '[0-9.]*' | head -1 || echo "0")
    
    echo "  - Transactions Submitted: $tx_submitted"
    echo "  - Submission Time: $tx_time seconds"
    
    if [ "$tx_time" != "0" ] && [ "$tx_time" != "" ]; then
        tps=$(echo "scale=2; $tx_submitted / $tx_time" | bc 2>/dev/null || echo "0")
        echo "  - WaterBear TPS: $tps"
    else
        echo "  - WaterBear TPS: Unable to calculate"
    fi
    
    echo ""
fi

# Check for crashes
total_crashes=$(find logs -name "*.log" -exec grep -l "panic" {} \; 2>/dev/null | wc -l)
echo "=== WaterBear Only Test Results ==="
echo "Total Server Crashes: $total_crashes"

if [ "$total_crashes" -eq 0 ]; then
    echo "✅ WaterBear component runs successfully without crashes!"
else
    echo "❌ WaterBear component still has crashes"
    echo "Latest crash details:"
    find logs -name "*.log" -exec grep -l "panic" {} \; 2>/dev/null | head -1 | xargs tail -10
fi

echo ""
echo "WaterBear only test completed."
